package com.hxd.otc.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hxd.otc.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 作  者 :  胡庆岭
 * 时  间 :  2018/5/10 15:04
 * 描  述 :  ${自定义标题栏}
 */
public class TitleBar extends RelativeLayout {

    @BindView(R.id.back_tv)
    TextView mBackText;
    @BindView(R.id.tv_title)
    TextView mTitleText;
    @BindView(R.id.right_tv)
    TextView mRightText;
    @BindView(R.id.right_tv2)
    TextView mRightText2;
    @BindView(R.id.parent_rl)
    RelativeLayout mParentGroup;
    @BindView(R.id.ll_right)
    LinearLayout mLlRight;
    private View mInflate;
    private Unbinder mBind;
    private boolean mIsFinish;
    private Context mContext;

    private boolean mIsFitSystemWindow;
    private int mTitleBarBackgroundColor;

    public void setOnTitleBarItemClickListener(OnTitleBarItemClickListener onTitleBarItemClickListener) {
        this.onTitleBarItemClickListener = onTitleBarItemClickListener;
    }

    private OnTitleBarItemClickListener onTitleBarItemClickListener;

    public TitleBar(Context context) {
        this(context, null);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
        initAttrs(attrs);
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TitleBar);
        if (typedArray != null && typedArray.getIndexCount() > 0) {
            setTitleText(typedArray.getString(R.styleable.TitleBar_title_bar_title));
            setTitleTextColor(typedArray.getColor(R.styleable.TitleBar_title_bar_title_color, ContextCompat.getColor(mContext, R.color.color_FF464E5F)));
            mBackText.setText(typedArray.getString(R.styleable.TitleBar_title_bar_back_title));

            mRightText.setText(typedArray.getString(R.styleable.TitleBar_title_bar_right_title));
            mRightText2.setText(typedArray.getString(R.styleable.TitleBar_title_bar_right2_title));
            mIsFinish = typedArray.getBoolean(R.styleable.TitleBar_title_bar_isfinish, true);

            mTitleBarBackgroundColor = typedArray.getColor(R.styleable.TitleBar_title_bar_background, ContextCompat.getColor(mContext, R.color.colorWhite));
            mParentGroup.setBackgroundColor(mTitleBarBackgroundColor);

            if (typedArray.getDrawable(R.styleable.TitleBar_title_bar_back_drawable) == null &&
                    typedArray.getBoolean(R.styleable.TitleBar_title_bar_is_show_back_icon, true)) {
                mBackText.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(), R.mipmap.icon_back), null, null, null);
            } else {
                mBackText.setCompoundDrawablesWithIntrinsicBounds(typedArray.getDrawable(R.styleable.TitleBar_title_bar_back_drawable), null, null, null);
            }
            mBackText.setVisibility(typedArray.getBoolean(R.styleable.TitleBar_title_bar_show_back, true) ? VISIBLE : GONE);
            if (!TextUtils.isEmpty(typedArray.getString(R.styleable.TitleBar_title_bar_back_title))) {
                mBackText.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            }

            mRightText.setCompoundDrawablesWithIntrinsicBounds(null, null, typedArray.getDrawable(R.styleable.TitleBar_title_bar_right_icon), null);
            mRightText2.setCompoundDrawablesWithIntrinsicBounds(null, null, typedArray.getDrawable(R.styleable.TitleBar_title_bar_right2_icon), null);
            Drawable rightDrawable = typedArray.getDrawable(R.styleable.TitleBar_title_bar_right_drawable);
            if (rightDrawable != null) {
                mRightText.setCompoundDrawablesWithIntrinsicBounds(null, null, rightDrawable, null);
            }
            Drawable rightDrawable2 = typedArray.getDrawable(R.styleable.TitleBar_title_bar_right2_drawable);
            if (rightDrawable2 != null) {
                mRightText2.setCompoundDrawablesWithIntrinsicBounds(null, null, rightDrawable2, null);
            }

            mRightText.setTextColor(typedArray.getColor(R.styleable.TitleBar_title_bar_right_title_color, ContextCompat.getColor(mContext, R.color.color_FF464E5F)));
            mRightText2.setTextColor(typedArray.getColor(R.styleable.TitleBar_title_bar_right2_title_color, ContextCompat.getColor(mContext, R.color.color_FF464E5F)));
            mTitleText.setTextColor(typedArray.getColor(R.styleable.TitleBar_title_bar_color, ContextCompat.getColor(mContext, R.color.color_FF464E5F)));
            mIsFitSystemWindow = typedArray.getBoolean(R.styleable.TitleBar_title_bar_is_fitsSystemWindows, true);
            typedArray.recycle();
        }
    }

    private void setTitleTextColor(int color) {
        mTitleText.setTextColor(color);
    }

    public void setTitleText(String title) {
        mTitleText.setText(title);
    }

    private void init() {
        mInflate = LayoutInflater.from(getContext()).inflate(R.layout.view_title_bar, this);
        mBind = ButterKnife.bind(this, mInflate);
    }


    @OnClick({R.id.back_tv, R.id.right_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_tv:
                if (onTitleBarItemClickListener != null) {
                    onTitleBarItemClickListener.onBack(view);
                }
                if (mIsFinish && mContext instanceof Activity) {
                    ((Activity) mContext).finish();
                }
                break;
            case R.id.right_tv:
                if (onTitleBarItemClickListener != null) {
                    onTitleBarItemClickListener.onRight(view);
                }
                break;
        }
    }

    public void setBg(int color) {
        mParentGroup.setBackgroundColor(color);
    }

    public interface OnTitleBarItemClickListener {
        void onBack(View v);

        void onRight(View v);
    }

    public TextView getTitle() {
        return mTitleText;
    }

    public TextView getRightText() {
        return mRightText;
    }

    public TextView getRightText2() {
        return mRightText2;
    }

    public LinearLayout getLlRight() {
        return mLlRight;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) && mIsFitSystemWindow) {
            View space = findViewById(R.id.space);
            RelativeLayout.LayoutParams layoutParams = (LayoutParams) space.getLayoutParams();
            layoutParams.height = getStatusBarHeight();
            space.setLayoutParams(layoutParams);
            space.setBackgroundColor(mTitleBarBackgroundColor);
        }
    }

    private int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
